import os


def use_file1():
    # utf8是打印文本文件
    file1 = open('1/file1.txt', encoding='utf8')#默认是rt模式，打开以阅读文本，但汉字还是得utf8
    text = file1.read(2)
    print(text)  # 人生
    text = file1.read(2)
    print(text)  # 苦短

    file1.close()


def use_file2():
    f = open('file3.text', mode='r', encoding='utf8')
    text2 = f.read()
    print(text2)
    # f.write('A') io.UnsupportedOperation: not writable
    f.close()


def use_file2_2():
    f = open('file2.text', mode='r+', encoding='utf8')
    # text2=f.read() # 读了光标就到最后了
    # print(text2)
    f.write('A')  # 指针放在的是文件的开头 文件内容会变成Abcdef
    f.close()


def use_file2_3():
    f = open('file2.text', mode='w+', encoding='utf8')
    # text2=f.read()
    # print(text2)
    f.write('A')  # 文件内容会变成A，会覆盖
    f.close()


def use_file2_4():
    f = open('file2.text', mode='a+', encoding='utf8')
    # text2=f.read()
    # print(text2)
    f.write('A')  # 文件内容会变成abcdefA,指针放在的是文件的结尾
    f.close()


def use_file2_5():
    f = open('file2.text', mode='rb+')
    # text2=f.read()
    # print(text2) # b'abcdef'
    # f.write('a')  #TypeError: a bytes-like object is required, not 'str'
    f.write(b'opqrst')  # 会覆盖掉abcdef,变成opqrst,b代表二进制写入，对于英文可以这样，汉字不行
    f.write('考研上岸'.encode('utf8'))  # 汉字的话要加encode才能以字节流形式写入
    f.close()


def use_file2_seek():
    f = open('file2.text', mode='r+', encoding='utf8')

    f.write('hello')  # 原file2是abcdef
    f.seek(3)  # 如果不移动光标，从hellof中只能print出f，光标在写入完的位置
    text2 = f.read()
    print(text2)  # lof 用了seek光标移动到了第三个字母，file2是hellof

    f.close()


def use_file4_seek():
    f = open('file4.text', mode='r+', encoding='utf8')

    f.seek(3)  # 存在汉字应该是偏移3个字节的倍数 ，读出来是'研上岸加油'
    text2 = f.read()
    print(text2)  # lof 用了seek光标移动到了第三个字母，file2是hellof

    f.close()


def use_file3_readline():
    f = open('file3.text', mode='r+', encoding='utf8')
    while True:
        text = f.readline()
        if not text:
            break
        print(text, end='')  # 不加end就会每行多空一行
        # 123
        # 456
        # 789
    f.close()


# 目录操作
# def new_file():
#     print(os.getcwd())
#     not os.mkdir('创建目录1')
#     os.mkdir('创建目录2')
#     os.rmdir('创建目录')
#     # os.chdir('mu')


# 小文件复制
def small_copy():
    file_read = open('file4.text')
    file_write = open('file5.text', 'w')

    text = file_read.read()
    file_write.write(text)

    file_read.close()
    file_write.close()


# 大文件逐行复制
def big_copy():
    file_read = open('file6.text',encoding='utf8')
    file_write = open('file7.text', 'w',encoding='utf8')

    while True:
        text = file_read.readline()
        if not text:
            break
        file_write.write(text)

    file_read.close()
    file_write.close()


# 重命名文件，删除文件
def rename_remove():
    os.rename('file2.text', '测试文件2.text')
    os.remove('用来删除用.text')


if __name__ == '__main__':
    # use_file1()
    use_file2()
    # use_file2_2()
    # use_file2_3()
    # use_file2_4()
    # use_file2_5()
    # use_file2_seek()
    # use_file3_readline()
    # # use_file4_seek()
    # new_file()
    # small_copy()
    # big_copy()
    # rename_remove()
